package org.roger.search;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.roger.queryparse.CustomQueryParser;

/**
 * 
 * @param analyzer 	分析用户输入字符串的分析器
 * @param field    	查询对应的域
 * @param search   	查询字符串
 */
public class ParserSearch extends LuceneSearchBase{
	
	private Analyzer analyzer;
	private String field;
	private String search;
	
	public ParserSearch(Analyzer analyzer,String field,String search){
		this.analyzer = analyzer;
		this.field = field;
		this.search = search;
	}

	@Override
	public Query getSearchQuery() {
//		QueryParser parser = new QueryParser(field,analyzer);
		CustomQueryParser parser = new CustomQueryParser(field,analyzer);
		
		Query query = null;
		try {
			query = parser.parse(search);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return query;
	}
	
	public static void main(String[] args) {
		ParserSearch search = new ParserSearch(new StandardAnalyzer(),"contents","id:[2 TO 4]");
		//ParserSearch search = new ParserSearch(new SplitSpotAnalyzer(),"contents","*");
		try {
			search.search("D:/index");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Sort getSort() {
		Sort sort = new Sort(new SortField("id",SortField.Type.INT,true));
		return sort;
	}
}
